package hierarchy

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/iHealth25/server/model/hierarchy/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type FdArticleAudioApi struct {
}

var fdArticleAudioService = service.ServiceGroupApp.HierarchyServiceGroup.FdArticleAudioService

// CreateFdArticleAudio 创建FdArticleAudio
// @Tags FdArticleAudio
// @Summary 创建FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleAudio true "创建FdArticleAudio"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /fdArticleAudio/createFdArticleAudio [post]
func (fdArticleAudioApi *FdArticleAudioApi) CreateFdArticleAudio(c *gin.Context) {
	var fdArticleAudio hierarchy.FdArticleAudio
	err := c.ShouldBindJSON(&fdArticleAudio)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleAudioService.CreateFdArticleAudio(&fdArticleAudio); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteFdArticleAudio 删除FdArticleAudio
// @Tags FdArticleAudio
// @Summary 删除FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleAudio true "删除FdArticleAudio"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /fdArticleAudio/deleteFdArticleAudio [delete]
func (fdArticleAudioApi *FdArticleAudioApi) DeleteFdArticleAudio(c *gin.Context) {
	var fdArticleAudio hierarchy.FdArticleAudio
	err := c.ShouldBindJSON(&fdArticleAudio)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleAudioService.DeleteFdArticleAudio(fdArticleAudio); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteFdArticleAudioByIds 批量删除FdArticleAudio
// @Tags FdArticleAudio
// @Summary 批量删除FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除FdArticleAudio"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /fdArticleAudio/deleteFdArticleAudioByIds [delete]
func (fdArticleAudioApi *FdArticleAudioApi) DeleteFdArticleAudioByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleAudioService.DeleteFdArticleAudioByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateFdArticleAudio 更新FdArticleAudio
// @Tags FdArticleAudio
// @Summary 更新FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.FdArticleAudio true "更新FdArticleAudio"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /fdArticleAudio/updateFdArticleAudio [put]
func (fdArticleAudioApi *FdArticleAudioApi) UpdateFdArticleAudio(c *gin.Context) {
	var fdArticleAudio hierarchy.FdArticleAudio
	err := c.ShouldBindJSON(&fdArticleAudio)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := fdArticleAudioService.UpdateFdArticleAudio(fdArticleAudio); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindFdArticleAudio 用id查询FdArticleAudio
// @Tags FdArticleAudio
// @Summary 用id查询FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchy.FdArticleAudio true "用id查询FdArticleAudio"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /fdArticleAudio/findFdArticleAudio [get]
func (fdArticleAudioApi *FdArticleAudioApi) FindFdArticleAudio(c *gin.Context) {
	var fdArticleAudio hierarchy.FdArticleAudio
	err := c.ShouldBindQuery(&fdArticleAudio)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if refdArticleAudio, err := fdArticleAudioService.GetFdArticleAudio(fdArticleAudio.ID, userID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"refdArticleAudio": refdArticleAudio}, c)
	}
}

// GetFdArticleAudioList 分页获取FdArticleAudio列表
// @Tags FdArticleAudio
// @Summary 分页获取FdArticleAudio列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.FdArticleAudioSearch true "分页获取FdArticleAudio列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /fdArticleAudio/getFdArticleAudioList [get]
func (fdArticleAudioApi *FdArticleAudioApi) GetFdArticleAudioList(c *gin.Context) {
	var pageInfo hierarchyReq.FdArticleAudioSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := fdArticleAudioService.GetFdArticleAudioInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// FdArticleLike 用id查询FdArticleAudio
// @Tags FdArticleAudio
// @Summary 用id查询FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /fdArticleAudio/like [get]
func (fdArticleAudioApi *FdArticleAudioApi) FdArticleLike(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleAudio, err := fdArticleAudioService.FdArticleLike(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		// 添加点赞积分相关
		err = UserService.AddPoints(userID, 1, 1)
		response.OkWithData(gin.H{"refdArticleAudio": refdArticleAudio}, c)
	}
}

// FdArticleCollect 收藏Audio
// @Tags FdArticleAudio
// @Summary 收藏Audio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /fdArticleAudio/collect [get]
func (fdArticleAudioApi *FdArticleAudioApi) FdArticleCollect(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if refdArticleAudio, err := fdArticleAudioService.FdArticleCollect(userID, uint(uid)); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"refdArticleAudio": refdArticleAudio}, c)
	}
}
